<template>
  <div class="edit">
    <el-container class="container">
      <el-aside width="200px">Aside</el-aside>
      <el-container>
        <el-header>
          <el-button @click="getData">获取数据</el-button>
        </el-header>
        <el-main>
          <graphObjectPage/>
        </el-main>
        <el-footer>Footer</el-footer>
      </el-container>
    </el-container>
  </div>
</template>

<script>
  import graphObjectPage from '@/components/mxGraph/index';

  export default {
    name: 'HelloWorld',
    components: {
      graphObjectPage
    },
    data() {
      return {}
    },
    methods: {
      getData() {
        const xml = mxUtils.getXml(window.myEditor.editor.getGraphXml());
        console.log(xml);

        //图片xml
        const imgExport = new mxImageExport();
        const scale = 1;
        const b = 1;
        const bounds = window.myEditor.editor.graph.getGraphBounds();
        const vs = window.myEditor.editor.graph.view.scale;

        // New image export
        const xmlDoc = mxUtils.createXmlDocument();
        console.log(xmlDoc);
        const root = xmlDoc.createElement('output');
        console.log(root);
        xmlDoc.appendChild(root);

        // Renders graph. Offset will be multiplied with state's scale when painting state.
        const xmlCanvas = new mxXmlCanvas2D(root);
        xmlCanvas.translate(Math.floor((b / scale - bounds.x) / vs), Math.floor((b / scale - bounds.y) / vs));
        xmlCanvas.scale(scale / vs);
        imgExport.drawState(window.myEditor.editor.graph.getView().getState(window.myEditor.editor.graph.model.root), xmlCanvas);

        // Puts request data together
        let svgXml, pngXml;
        const w = Math.ceil(bounds.width * scale / vs + 2 * b);
        const h = Math.ceil(bounds.height * scale / vs + 2 * b);
        svgXml = mxUtils.getXml(window.myEditor.editor.graph.getSvg(window.myEditor.editor.graph.background, 1, 0));
        pngXml = mxUtils.getXml(root);
        console.log(pngXml);
        if (window.myEditor.editor.graph.getDefaultParent() && window.myEditor.editor.graph.getDefaultParent().children && window.myEditor.editor.graph.getDefaultParent().children.length === 0) {
          svgXml = '';
          pngXml = '';
        }
        myEditor.actions.actions.save.setEnabled(false);
        myEditor.editor.graph.setEnabled(false);

        let data = {
          dataXml: encodeURIComponent(xml),
          modelId: window.MODELID,
          svgXml: encodeURIComponent(svgXml),
          pngXml: encodeURIComponent(pngXml),
          width: w,
          height: h,
          bgUrl: ''
        };

        console.log(data)
      },
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
  .edit {
    width: 100%;
    height: 100vh
  }

  .edit .container {
    height: 100%;
  }

  .el-header, .el-footer {
    background-color: #B3C0D1;
    color: #333;
    line-height: 60px;
  }

  .el-aside {
    background-color: #D3DCE6;
    color: #333;
    text-align: center;
    line-height: 200px;
  }

  .el-main {
    background-color: #E9EEF3;
    color: #333;
  }

  body > .el-container {
    margin-bottom: 40px;
  }

  .el-container:nth-child(5) .el-aside,
  .el-container:nth-child(6) .el-aside {
    line-height: 260px;
  }

  .el-container:nth-child(7) .el-aside {
    line-height: 320px;
  }
</style>
